package com.dwxt.sport.business.service;

import com.dwxt.common.base.StatusCode;
import com.dwxt.common.constant.VariableConstants;
import com.dwxt.common.util.SnowIdUtil;
import com.dwxt.sport.business.dao.WisdomVenuesEquipmentMapper;
import com.dwxt.sport.business.dao.WisdomVenuesMapper;
import com.dwxt.sport.business.entity.WisdomVenues;
import com.dwxt.sport.business.entity.WisdomVenuesEquipment;
import com.dwxt.sport.business.vo.wisdomVenues.InsertOrUpdateWisdomVenuesEquipmentVo;
import com.dwxt.sport.business.vo.wisdomVenues.InsertOrUpdateWisdomVenuesVo;
import com.dwxt.sport.business.vo.wisdomVenues.SelectWisdomVenuesVo;
import com.dwxt.sport.constant.CommonConstant;
import com.dwxt.sport.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 智慧场馆
 *
 * @author Kuang.JiaZhuo
 * @date 2020-07-31 15:24
 */
@Service
public class WisdomVenuesService {

    @Resource
    WisdomVenuesMapper wisdomVenuesMapper;

    @Resource
    WisdomVenuesEquipmentMapper equipmentMapper;

    /**
     * @方法名：insertOrUpdateWisdomVenues
     * @描述： 新增或修改智慧场馆
     * @作者： kjz
     * @日期： Created in 2020/8/3 10:01
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateWisdomVenues(InsertOrUpdateWisdomVenuesVo insertOrUpdateVo) {
        String id = insertOrUpdateVo.getId();

        WisdomVenues wisdomVenues = new WisdomVenues()
                .setName(insertOrUpdateVo.getName()) //设备名称
                .setRecommendedCoverage(insertOrUpdateVo.getRecommendedCoverage()) //建议覆盖率
                .setUnitType(insertOrUpdateVo.getUnitType()) //设备型号
                .setEquipmentsImgUrl(insertOrUpdateVo.getEquipmentsImgUrl()); //设备图片地址

        if(null==id || id.trim().length()==0){
            //新增
            String streetId = insertOrUpdateVo.getStreetId();
            if(null==streetId || streetId.trim().length()==0){
                throw new CustomException(StatusCode.STREET_ID_IS_NULL);
            }

            wisdomVenues.setId(SnowIdUtil.getInstance().nextId())
                    .setCreateTime(LocalDateTime.now()).setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setStreetId(streetId); //街道id
            return wisdomVenuesMapper.insert(wisdomVenues);
        }else{
            //更新
            wisdomVenues.setUpdateTime(LocalDateTime.now()).setId(id);
            return wisdomVenuesMapper.updateByPrimaryKeySelective(wisdomVenues);
        }
    }

    /**
     * @方法名：selectWisdomVenuesByCriteria
     * @描述： 按条件查询智慧场馆列表(带分页)
     * @作者： kjz
     * @日期： Created in 2020/8/3 10:09
     */
    public PageInfo<WisdomVenues> selectWisdomVenuesByCriteria(SelectWisdomVenuesVo selectVo) {
        PageHelper.startPage(selectVo.getPageNum(), selectVo.getPageSize());
        List<WisdomVenues> list = wisdomVenuesMapper.selectWisdomVenuesByCriteria(selectVo);
        list.forEach(wisdomVenues -> {
            wisdomVenues.setEquipmentsImgUrl(CommonConstant.FILE_SERVER+wisdomVenues.getEquipmentsImgUrl());
        });
        PageInfo<WisdomVenues> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    /**
     * @方法名：deleteWisdomVenuesByIds
     * @描述： 根据ids批量删除智慧场馆
     * @作者： kjz
     * @日期： Created in 2020/8/3 10:18
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteWisdomVenuesByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");

        return this.wisdomVenuesMapper.deleteWisdomVenuesByIds(idArray);
    }

    /**
     * @方法名：insertOrUpdateWisdomVenuesEquipment
     * @描述： 新增 或 更新【智慧场馆】设备关联
     * @作者： kjz
     * @日期： Created in 2020/8/3 10:31
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateWisdomVenuesEquipment(InsertOrUpdateWisdomVenuesEquipmentVo insertOrUpdateVo) {

        String id = insertOrUpdateVo.getId();
        WisdomVenuesEquipment wisdomVenuesEquipment = new WisdomVenuesEquipment()
                .setEquipmentId(insertOrUpdateVo.getEquipmentId()) //设备id
                .setTodayPeople(insertOrUpdateVo.getTodayPeople()) //今日人次
                .setPeopleAlways(insertOrUpdateVo.getPeopleAlways()) //总人次
                .setDeviceStates(insertOrUpdateVo.getDeviceStates()); //设备状态
        int result = 0;
        String wisdomVenuesId = insertOrUpdateVo.getWisdomVenuesId();
        if (null == id || id.trim().length() == 0) {
            //新增
            if (null == wisdomVenuesId || wisdomVenuesId.trim().length() == 0) {
                throw new CustomException(StatusCode.WISDOM_VENUES_ID_IS_NULL);
            }
            wisdomVenuesEquipment.setId(SnowIdUtil.getInstance().nextId())
                    .setCreateTime(LocalDateTime.now()).setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setWisdomVenuesId(wisdomVenuesId); //智慧场馆关联id

            result = equipmentMapper.insert(wisdomVenuesEquipment);

        } else {
            //更新
            wisdomVenuesEquipment.setUpdateTime(LocalDateTime.now()).setId(id);
            result = equipmentMapper.updateByPrimaryKeySelective(wisdomVenuesEquipment);
        }

        if(result>0){
            this.updateWisdomVenues(wisdomVenuesId);
        }

        return result;
    }

    /**
     * @方法名：updateWisdomVenues
     * @描述： 新增、更新、删除智慧场馆后重新插入需计算得到的数据
     * @作者： kjz
     * @日期： Created in 2020/8/5 14:15
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateWisdomVenues(String wisdomVenuesId) {
        HashMap map = equipmentMapper.selectStatisticsByWisdomVenuesId(wisdomVenuesId);

       WisdomVenues wisdomVenues = new WisdomVenues()
                .setAmount(Integer.valueOf(map.get("amount").toString()))
                .setUpdateTime(LocalDateTime.now())
                .setId(wisdomVenuesId);
        return wisdomVenuesMapper.updateByPrimaryKeySelective(wisdomVenues);
    }

    /**
     * @方法名：deleteWisdomVenuesEquipmentByIds
     * @描述： 根据ids批量删除【智慧场馆】设备关联
     * @作者： kjz
     * @日期： Created in 2020/8/3 11:13
     */
    @Transactional(rollbackFor = Exception.class)
    public int deleteWisdomVenuesEquipmentByIds(String ids) {
        if(ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");

        int result = equipmentMapper.deleteWisdomVenuesEquipmentByIds(idArray);

        if(result>0){
            String id = idArray[0];
            String wisdomVenuesId = this.equipmentMapper.selectByPrimaryKey(id).getWisdomVenuesId();
            this.updateWisdomVenues(wisdomVenuesId);
        }

        return result;
    }

    /**
     * @方法名：selectWisdomVenuesEquipmentByPage
     * @描述： 根据【智慧场馆】的id查询关联设备的列表(带分页)
     * @作者： kjz
     * @日期： Created in 2020/8/3 11:17
     */
    public PageInfo<WisdomVenuesEquipment> selectWisdomVenuesEquipmentByPage(Integer pageNum, Integer pageSize, String wisdomVenuesId) {
        PageHelper.startPage(pageNum==null?1:pageNum, pageSize==null?10:pageSize);
        List<WisdomVenuesEquipment> list = equipmentMapper.selectWisdomVenuesEquipmentByPage(wisdomVenuesId);
        PageInfo<WisdomVenuesEquipment> pageInfo = new PageInfo(list);
        return pageInfo;
    }
}
